package com.movecar.ui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import cn.jpush.android.api.JPushInterface;

import com.google.gson.Gson;
import com.movecar.R;
import com.movecar.model.CloudBeansBean;
import com.movecar.model.SelfInfoBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.PopupWindowAnimation;
import com.movecar.utils.RequestResultCallback;
import com.movecar.widget.CustomProgressDialog;

/**
 * 更多-个性定制
 * 
 * @author Zhao.JQ
 * 
 */

public class PersonalizedCustomActivity extends Activity implements OnClickListener {

	private ImageView mImageView1;
	private ImageView mImageView2;
	private String nightMode;
	private String notifyMode;
	private SelfInfoBean mSelfInfoBean;
	private CloudBeansBean mCloudBeansBean;
	private CustomProgressDialog progressDialog = null;
	private View mainView;
	private LayoutInflater layoutInflater;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mainView = layoutInflater.inflate(R.layout.personalized_custom, null);
		setContentView(mainView);
		startProgressDialog();
		getInfo();
		initView();

	}

	private void initView() {

		mImageView1 = (ImageView) findViewById(R.id.img1);
		mImageView2 = (ImageView) findViewById(R.id.img2);
		mImageView1.setOnClickListener(this);
		mImageView2.setOnClickListener(this);
		Button back = (Button) findViewById(R.id.btn_left);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("个性定制");
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_left:
			finish();
			break;
		case R.id.img1:
			if ("1".equals(nightMode)) {
				mImageView1.setImageDrawable(this.getResources().getDrawable(R.drawable.switch_off));
				setNotifyMode();
				nightMode = "2";
			} else {
				mImageView1.setImageDrawable(this.getResources().getDrawable(R.drawable.switch_on));
				setNightMode();
				nightMode = "1";
			}
			setInfo();
			break;
		case R.id.img2:
			if ("1".equals(notifyMode)) {
				mImageView2.setImageDrawable(this.getResources().getDrawable(R.drawable.switch_off));
				setUnNotifyMode();
				notifyMode = "2";
			} else {
				mImageView2.setImageDrawable(this.getResources().getDrawable(R.drawable.switch_on));
				setNotifyMode();
				notifyMode = "1";
			}
			setInfo();
			break;
		default:
			break;
		}

	}

	private void setNightMode() {

		Set<Integer> days = new HashSet<Integer>();
		days.add(1);
		days.add(2);
		days.add(3);
		days.add(4);
		days.add(5);
		days.add(6);
		days.add(0);
		JPushInterface.setPushTime(getApplicationContext(), days, 6, 23);
	}

	private void setNotifyMode() {

		Set<Integer> days = new HashSet<Integer>();
		days.add(1);
		days.add(2);
		days.add(3);
		days.add(4);
		days.add(5);
		days.add(6);
		days.add(0);
		JPushInterface.setPushTime(getApplicationContext(), days, 0, 23);
	}

	private void setUnNotifyMode() {

		Set<Integer> days = new HashSet<Integer>();
		days.add(1);
		days.add(2);
		days.add(3);
		days.add(4);
		days.add(5);
		days.add(6);
		days.add(0);
		JPushInterface.setPushTime(getApplicationContext(), days, 0, 0);
	}

	@SuppressWarnings("unchecked")
	private void getInfo() {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryModeNew1"));
		// 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {
				stopProgressDialog();
				String result = (String) object;
				Gson gson = new Gson();
				try {
					mSelfInfoBean = gson.fromJson(result, SelfInfoBean.class);
				} catch (Exception e) {
					return;
				}

				if ("".equals(mSelfInfoBean.getError()) || null == mSelfInfoBean.getError()) {
					if ("".equals(mSelfInfoBean.getFail()) || null == mSelfInfoBean.getFail()) {

						nightMode = mSelfInfoBean.getNightMode();
						notifyMode = mSelfInfoBean.getNotifyMode();
						if ("1".equals(nightMode)) {
							mImageView1.setVisibility(View.VISIBLE);
							mImageView1.setImageDrawable(PersonalizedCustomActivity.this.getResources().getDrawable(
									R.drawable.switch_on));
							setNightMode();
						} else {
							mImageView1.setVisibility(View.VISIBLE);
							mImageView1.setImageDrawable(PersonalizedCustomActivity.this.getResources().getDrawable(
									R.drawable.switch_off));
							setNotifyMode();
						}
						if ("1".equals(notifyMode)) {
							mImageView2.setVisibility(View.VISIBLE);
							mImageView2.setImageDrawable(PersonalizedCustomActivity.this.getResources().getDrawable(
									R.drawable.switch_on));
						} else {
							mImageView2.setVisibility(View.VISIBLE);
							mImageView2.setImageDrawable(PersonalizedCustomActivity.this.getResources().getDrawable(
									R.drawable.switch_off));
						}

					} else {
						Alert.info(PersonalizedCustomActivity.this, mSelfInfoBean.getFail());
					}
				} else {
					Alert.info(PersonalizedCustomActivity.this, mSelfInfoBean.getError());
				}

			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.info(PersonalizedCustomActivity.this, "与服务器连接失败，请检查网络");

			}
		}, SelfInfoBean.class, AppConfig.USER).execute(params);
	}

	@SuppressWarnings("unchecked")
	private void setInfo() {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "updateModeNew1"));
		// 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("nightModeStr", nightMode));
		params.add(new BasicNameValuePair("notifyModeStr", notifyMode));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {
				String result = (String) object;
				Gson gson = new Gson();
				try {
					mCloudBeansBean = gson.fromJson(result, CloudBeansBean.class);
				} catch (Exception e) {
					return;
				}
				if (null != mCloudBeansBean) {
					if ("0".equals(mCloudBeansBean.getResultCode())) {
						if (!TextUtils.isEmpty(mCloudBeansBean.getCoinAlert())) {
							new PopupWindowAnimation(mainView, PersonalizedCustomActivity.this);
						}
						Alert.info(PersonalizedCustomActivity.this, "设置成功");
						finish();

					} else {
						Alert.info(PersonalizedCustomActivity.this, "设置失败");
					}

				} else {
					Alert.info(PersonalizedCustomActivity.this, "系统异常");
				}

			}

			@Override
			public void onFail(String exception) {
				Alert.info(PersonalizedCustomActivity.this, "与服务器连接失败，请检查网络");

			}
		}, SelfInfoBean.class, AppConfig.USER).execute(params);
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}
}
